﻿using NewChillers.Helpers;
using NewChillers.Models;
using NewChillers.Utils;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace NewChillers.ViewModels
{
    public class AlarmParameterSettingViewModel : ObservableObject
    {
        #region 属性

        private AlarmParameterModel _alarmParameterModel;
        private UserModel _currentUser;
        /// <summary>
        /// 定义关闭事件
        /// </summary>
        public event Action RequestClose;


        /// <summary>
        /// 高压报警值
        /// </summary>
        public double HighPressure
        {
            get => _alarmParameterModel.HighPressure;
            set
            {
                if (_alarmParameterModel.HighPressure != value)
                {
                    _alarmParameterModel.HighPressure = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 低压报警值
        /// </summary>
        public double LowPressure
        {
            get => _alarmParameterModel.LowPressure;
            set
            {
                if (_alarmParameterModel.LowPressure != value)
                {
                    _alarmParameterModel.LowPressure = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 高温报警值
        /// </summary>
        public double HighTemperature
        {
            get => _alarmParameterModel.HighTemperature;
            set
            {
                if (_alarmParameterModel.HighTemperature != value)
                {
                    _alarmParameterModel.HighTemperature = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 低温报警值
        /// </summary>
        public double LowTemperature
        {
            get => _alarmParameterModel.LowTemperature;
            set
            {
                if (_alarmParameterModel.LowTemperature != value)
                {
                    _alarmParameterModel.LowTemperature = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 高流量报警值
        /// </summary>
        public double HighFlow
        {
            get => _alarmParameterModel.HighFlow;
            set
            {
                if (_alarmParameterModel.HighFlow != value)
                {
                    _alarmParameterModel.HighFlow = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 低流量报警值
        /// </summary>
        public double LowFlow
        {
            get => _alarmParameterModel.LowFlow;
            set
            {
                if (_alarmParameterModel.LowFlow != value)
                {
                    _alarmParameterModel.LowFlow = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 报警延时（秒）
        /// </summary>
        public int AlarmDelay
        {
            get => _alarmParameterModel.AlarmDelay;
            set
            {
                if (_alarmParameterModel.AlarmDelay != value)
                {
                    _alarmParameterModel.AlarmDelay = value;
                    RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// 方法
        /// </summary>
        public ICommand CancelCommand { get; }//取消
        public ICommand ConfirmCommand { get; }//确认
        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userModel"></param>
        public AlarmParameterSettingViewModel(UserModel userModel)
        {
            _currentUser = userModel;
            _alarmParameterModel = new AlarmParameterModel();
            //加载 默认报警设置值
            LoadDefaultValues();

            CancelCommand = new RelayCommand(ExecuteCancel);

            ConfirmCommand = new RelayCommand(ExecuteConfirm);
        }

        /// <summary>
        /// 加载默认报警设置值
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void LoadDefaultValues()
        {
            // 从Model加载默认值到ViewModel字段
            HighPressure = _alarmParameterModel.HighPressure;
            LowPressure = _alarmParameterModel.LowPressure;
            HighTemperature = _alarmParameterModel.HighTemperature;
            LowTemperature = _alarmParameterModel.LowTemperature;
            HighFlow = _alarmParameterModel.HighFlow;
            LowFlow = _alarmParameterModel.LowFlow;
            AlarmDelay = _alarmParameterModel.AlarmDelay;
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ExecuteCancel(object obj)
        {
            //询问是否确认取消
            var result = MessageBox.Show("确定要取消修改吗？", "确认取消", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.Yes)
            {
                RequestClose?.Invoke();
            }
            
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void ExecuteConfirm(object obj)
        {
            try
            {
                // 验证输入数据
                //if (!ValidateInput())
                //{
                //    return;
                //}

                // 保存到配置文件
                _alarmParameterModel.SaveToConfig();

                MsgBoxHelper.Info("报警参数保存成功");

                // 记录操作日志
                LogHelper.Info(this.GetType(), "[保存报警参数]|用户保存报警参数", _currentUser?.UserAccount);

                RequestClose?.Invoke();
            }
            catch (Exception ex)
            {
                MsgBoxHelper.Error($"保存参数失败: {ex.Message}");
                LogHelper.Error(this.GetType(), "[保存报警参数]|保存报警参数失败", ex, _currentUser?.UserAccount);
            }
        }


        
    }
}
